package com.code.springboot.controller;

import com.baomidou.mybatisplus.core.conditions.interfaces.Func;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.code.springboot.common.BaseContext;
import com.code.springboot.common.R;
import com.code.springboot.dto.DishDto;
import com.code.springboot.dto.OrdersDto;
import com.code.springboot.entity.Category;
import com.code.springboot.entity.Dish;
import com.code.springboot.entity.Orders;
import com.code.springboot.entity.User;
import com.code.springboot.service.OrdersService;
import com.code.springboot.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @program: springboot
 * @create: 2022-08-14 16:56
 **/
@Slf4j
@RestController
@RequestMapping("/order")
public class OrdersController {
    @Autowired
    private OrdersService ordersService;

    @Autowired
    private UserService userService;


    private OrdersDto ordersDto;
    //用户下单
    @PostMapping("/submit")
    public R<String > submit(@RequestBody Orders orders){
        ordersService.submit(orders);
        return R.success("下单成功");
    }

    //分页查询
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, Long number, String beginTime,String endTime){
        log.info("page={},pageSize={},number={},beginTime={},endTime={}",page,pageSize,number,beginTime,endTime);

        //构造分页构造器
        Page<Orders> pageInfo = new Page<>(page,pageSize);
        Page<OrdersDto> ordersDtoPage = new Page<>();
        //添加筛选条件
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.eq(number != null,Orders::getNumber,number);

        queryWrapper.ge(beginTime != null,Orders::getOrderTime, beginTime);

        queryWrapper.le(endTime != null,Orders::getOrderTime,endTime);

        queryWrapper.orderByAsc(Orders::getOrderTime);

        ordersService.page(pageInfo,queryWrapper);

        BeanUtils.copyProperties(pageInfo,ordersDtoPage,"records");

        List<Orders> records = pageInfo.getRecords();
        List<OrdersDto> list = records.stream().map((item)->{

            OrdersDto ordersDto = new OrdersDto();
            BeanUtils.copyProperties(item,ordersDto);

            Long userId = item.getUserId();

            User user = userService.getById(userId);

            if(user != null){
                String userName = user.getName();
                ordersDto.setUserName(userName);
            }
            return ordersDto;
        }).collect(Collectors.toList());

        ordersDtoPage.setRecords(list);
        return R.success(ordersDtoPage);
    }



    @GetMapping("/userPage")
    public R<Page> page(int page,int pageSize){
        Page<Orders> pageInfo = new Page<>(page,pageSize);

        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.eq(Orders::getUserId,BaseContext.getCurrentId());

        queryWrapper.orderByDesc(Orders::getOrderTime);

        Page<Orders> page1 = ordersService.page(pageInfo, queryWrapper);

        return R.success(page1);
    }


    @PutMapping
    public R<String> status(@RequestBody Orders orders){
        Orders one = ordersService.getById(orders.getId());

        one.setStatus(orders.getStatus());

        ordersService.updateById(one);

        return R.success("修改成功");
    }
}
